﻿// -----------------------------------------------------------------------
// <copyright file="TooltipController.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Website.Controllers
{
	#region Usings

	using System;
	using System.Collections.Generic;
	using System.Web.Mvc;

	using MBOP.Core.DTO;
	using MBOP.Core.Entities.Common;
	using MBOP.Core.Entities.Library;
	using MBOP.Core.Interfaces.Services;
	using MBOP.Website.CustomAttributes;
	using MBOP.Website.Infrastructure.Mappers;
	using MBOP.Website.ViewModels.Common;
	using MBOP.Website.ViewModels.Tooltips;

	#endregion

	/// <summary>
	/// Provides methods that respond to HTTP requests that are made to tooltip operations
	/// </summary>
	public class TooltipController : Controller
	{
		#region Constants

		/// <summary>
		/// Represents person information tooltip partial view name.
		/// </summary>
		private const string PARTIAL_VIEW_PERSON_INFO = @"~/Views/Tooltips/PersonInfo.cshtml";

		/// <summary>
		/// Represents person time tooltip partial view name.
		/// </summary>
		private const string PARTIAL_VIEW_PERSON_TIME_INFO = @"~/Views/Tooltips/PersonTimeInfo.cshtml";

		/// <summary>
		/// Represents common tooltip partial view name.
		/// </summary>
		private const string PARTIAL_VIEW_GENERIC_INFO = @"~/Views/Tooltips/GenericInfo.cshtml";

		#endregion

		#region Private Fields

		/// <summary>
		/// Represents person information service.
		/// </summary>
		private IPersonInformationService _personInfoService;

		/// <summary>
		/// Represents book service.
		/// </summary>
		private IBookService _bookService;

		/// <summary>
		/// Represents person information mapper.
		/// </summary>
		private PersonInfoMapper _personInfoMapper;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="TooltipController"/> class.
		/// </summary>
		/// <param name="personInfoService">The <see cref="IPersonInformationService"/> implementer</param>
		/// <param name="bookService">The <see cref="IBookService"/> implementer</param>
		public TooltipController(IPersonInformationService personInfoService, IBookService bookService)
		{
			this._personInfoService = personInfoService;
			this._bookService = bookService;
			this._personInfoMapper = new PersonInfoMapper(personInfoService);
		}

		#endregion

		#region Actions

		/// <summary>
		/// Processes getting person's information request.
		/// </summary>
		/// <param name="personID">The person's unique name.</param>
		/// <returns>The partial view result, which encapsulates information of the result processing.</returns>
		public PartialViewResult GetPersonInfoTooltip(string personID)
		{
			PersonInfo personInfo = this._personInfoService.GetPersonInfo(personID, PersonFindingType.ID, true);

			PersonInfoViewModel personInfoViewModel = this._personInfoMapper.MapPersonInfo(personInfo);

			return this.PartialView(PARTIAL_VIEW_PERSON_INFO, personInfoViewModel);
		}

		/// <summary>
		/// Processes getting person time tooltip request.
		/// </summary>
		/// <param name="personID">The person's unique name.</param>
		/// <param name="subscriptionSet">The subscription set.</param>
		/// <param name="bookID">The unique book number.</param>
		/// <returns>The partial view result, which encapsulates information of the result processing.</returns>
		[HandleNullModel]
		public PartialViewResult GetPersonTimeTooltip(string personID, string subscriptionSet, int bookID)
		{
			Book book = this._bookService.GetBookByID(bookID);

			BookSubscription bookSubscription = this._bookService.GetCurrentPersonSubscription(book, personID, subscriptionSet);

			TimeSpan personStateTime = this._bookService.GetPersonTime(book, personID, subscriptionSet);

			PersonTimeInfoViewModel personTimeInfoViewModel = this._personInfoMapper.MapPersonTimeInfo(bookSubscription, personStateTime);

			return this.PartialView(PARTIAL_VIEW_PERSON_TIME_INFO, personTimeInfoViewModel);
		}

		/// <summary>
		/// Processes getting common tooltip request.
		/// </summary>
		/// <param name="tooltipType">The tooltip type.</param>
		/// <returns>The partial view result, which encapsulates information of the result processing.</returns>
		[HandleNullModel]
		public PartialViewResult GetGenericTooltip(TooltipType tooltipType)
		{
			PartialViewResult result = PartialView(PARTIAL_VIEW_GENERIC_INFO, tooltipType);

			return result;
		}
		#endregion
	}
}
